package com.qkl.xiatang.app.utils;

import com.alibaba.fastjson.JSON;
import com.qkl.xiatang.app.dto.user.CompanyDTO;
import com.qkl.xiatang.app.dto.user.UserOutDTO;
import com.qkl.xiatang.app.entity.user.AppUserInfo;
import com.qkl.xiatang.app.utils.appenum.AppUserDutiesEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class AppUserUtils {

    public static UserOutDTO copyUserDTO(AppUserInfo userInfo){
        UserOutDTO outDTO = new UserOutDTO();
        outDTO.setAvatar(StringUtils.trimToEmpty(userInfo.getAvatar()));
        outDTO.setToken(userInfo.getToken());
        outDTO.setUsername(StringUtils.trimToEmpty(userInfo.getUsername()));
        outDTO.setUserId(userInfo.getId());
        outDTO.setIdentity(userInfo.getIdentity());
        outDTO.setName(StringUtils.trimToEmpty(userInfo.getName()));
        outDTO.setStatus(userInfo.getStatus());
        outDTO.setCompanyId(userInfo.getCompanyId());
        CompanyDTO companyDTO = userInfo.companyInfo();
        if(null != companyDTO){
            outDTO.setCompanyName(companyDTO.getName());
            outDTO.setCompanyLogo(companyDTO.getLogo());
        }
        String duties = userInfo.getIdentity();
        outDTO.setFeed(AppUserDutiesEnum.isFeed(duties));
        outDTO.setAlloc(AppUserDutiesEnum.isAlloc(duties));
        outDTO.setCheck(AppUserDutiesEnum.isCheck(duties));
        outDTO.setWeigh(AppUserDutiesEnum.isWeigh(duties));
        outDTO.setAdmin(AppUserDutiesEnum.isAdmin(duties));
        outDTO.setHolder(AppUserDutiesEnum.isHolder(duties));
        outDTO.setObserve(AppUserDutiesEnum.isObserve(duties));
        if(StringUtils.isNotEmpty(userInfo.getAdditInfo())){
            outDTO.setAdditInfo(JSON.parseObject(userInfo.getAdditInfo(), Map.class));
        }
        if(ObjectUtils.isEmpty(outDTO.getAdditInfo())){
            outDTO.setAdditInfo(new HashMap<>());
        }
        return outDTO;
    }
}
